using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Xml.Linq;

namespace Controllers
{
    public class SeriesController : ApiController
    {

        public IEnumerable<Series> Get()
        {
            API.Logger.Log(this, "Sending Series");
            SoccerAPI soccerAPI = new SoccerAPI();
            var series = soccerAPI.GetSeries();
            foreach (int ID in series.Select(x => x.ID))
            {
                soccerAPI.GetUpcomingMatches(ID);
            }
            return series;
        }

        public Series Get(string ID)
        {
            API.Logger.Log(this, "Sending serie " + ID);
            SoccerAPI soccerAPI = new SoccerAPI();
            Series Serie = soccerAPI.GetSeries().Where(serie => serie.ID == int.Parse(ID)).FirstOrDefault();
            if (Serie != null)
            {
                return Serie;
            }
            else
            {
                throw new HttpResponseException(System.Net.HttpStatusCode.NotFound);
            }
        }

         [HttpGet]
        public IEnumerable<Match> UpcomingMatches(string ID)
        {
            API.Logger.Log(this, "Sending upcoming matches " + ID);
            SoccerAPI soccerAPI = new SoccerAPI();
            var matches = soccerAPI.GetUpcomingMatches(int.Parse(ID));
            if (matches != null) { return matches; }
            else
            {
                throw new HttpResponseException(System.Net.HttpStatusCode.NoContent);
            }
        }

        [HttpGet]
        public IEnumerable<TeamResults> Results(string ID)
        {
            API.Logger.Log(this, "Sending Results " + ID);
            SoccerAPI soccerAPI = new SoccerAPI();
            return soccerAPI.GetScoreTable(int.Parse(ID));
        }

    }
}